<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Http\Requests\BasicRequest;
use App\Models\Log;
use App\Models\ShopSeckillGoods;
use App\Http\Requests\GoodsDetailRequest;

use App\Models\ShopGoods;
use App\Models\ShopGroupGoods;
use App\Models\ShopGoodsCollection;
use App\Models\ShopStore;
use Illuminate\Support\Facades\DB;

class ShopController extends CommonController
{

	public function seckill_list(BasicRequest $request){
        $village_id = $request->input('village_id');

        $return = ShopSeckillGoods::Sales()
            ->leftJoin('shop_goods_village as village','village.goods_id','=','shop_seckill_goods.id')
            ->where('village.village_id','=',$village_id)
            ->where('village.goods_type','=',1)
            ->Field()->List();

        return json_success('OK',$return);
	}

    public function group_list(BasicRequest $request){
        $village_id = $request->input('village_id');
        $return = ShopGroupGoods::Sales()
            ->leftJoin('shop_goods_village as village','village.goods_id','=','shop_group_goods.id')
            ->where('village.village_id','=',$village_id)
            ->where('village.goods_type','=',2)->Field()->List();
        return json_success('OK',$return);
    }
	
	
    //商品详情
    public function goods_detail(GoodsDetailRequest $request){
        $goods_id = $request->input('goods_id');
        $goods_type = $request->input('goods_type');
        $this->check_login();
        if($goods_type==1){
            $goods_detail=ShopSeckillGoods::getGoodsDetail($goods_id);
        }else if($goods_type==2){
            $goods_detail=ShopGroupGoods::getGoodsDetail($goods_id);
        }else{
            $goods_detail=ShopGoods::getGoodsDetail($goods_id);
            if(!$goods_detail){
                return json_error('找不到该商品');
            }
            $goods_detail->setting =  ShopStore::where(['id'=>$goods_detail->store_id])->select('work_start_time','work_end_time','is_open','start_amount','shipping_amount')->first();
            $goods_type = $goods_detail->goods_type;
        }
        if(!$goods_detail){
            return json_error('找不到该商品');
        }
        Log::addGoodsView($goods_id, $goods_type,$this->uid,$goods_detail->store_id);
        $goods_detail->head_image=DB::table('shop_goods_img')->select('img_url')->where(['goods_type'=>$goods_type,'img_type'=>1,'goods_id'=>$goods_id])->orderBy('img_serial','asc')->get()->pluck('img_url');
        $goods_detail->detail_image=DB::table('shop_goods_img')->select('img_url')->where(['goods_type'=>$goods_type,'img_type'=>2,'goods_id'=>$goods_id])->orderBy('img_serial','asc')->get()->pluck('img_url');
        $goods_sale = DB::table('shop_goods_sale')->select('sale_num','jsale_num')->where(['goods_type'=>$goods_type,'goods_id'=>$goods_id])->first();
        if($goods_sale) { $goods_detail->sale_num = $goods_sale->sale_num + $goods_sale->jsale_num; }else{$goods_detail->sale_num = 0; }//销售数量

        $is_collection = DB::table('shop_goods_collection')->where(['goods_id'=>$goods_id,'goods_type'=>$goods_type,'uid'=>$this->uid])->value('is_collection');
        if($is_collection==1){
            $goods_detail->is_collection = 1;
        }else{
            $goods_detail->is_collection = 0;
        }

        return json_success('OK',$goods_detail);
    }
    //商品收藏
    public function goods_collection(BasicRequest $request){
        $this->check_login();
        $goods_id = $request->input('goods_id');

        $goods_type = $request->input('goods_type');
        $is_collection=$request->input('is_collection');

        if(!isset($is_collection)){
            return json_error('参数错误');
        }
        if(!$goods_id || !$goods_type ){
            return json_error('参数错误');
        }

        $this->check_login();

        if($goods_type==1){
            $goods_detail=ShopSeckillGoods::getGoodsDetail($goods_id);
        }else if($goods_type==2){
            $goods_detail=ShopGroupGoods::getGoodsDetail($goods_id);
        }else{
            $goods_detail=null;
        }
        $insert['is_collection']=$request->input('is_collection');
        $insert['goods_id']=$goods_id;
        $insert['goods_type']=$goods_type;
        $insert['goods_image']=$goods_detail->goods_image;
        $insert['goods_name']=$goods_detail->goods_name;
        $insert['goods_price']=$goods_detail->goods_price;
        $insert['uid']=$this->uid;
        $return = ShopGoodsCollection::updateOrCreate(['goods_id'=>$goods_id,'uid'=>$this->uid],$insert);

        if($return) {
            return json_success('OK', ['is_collection' => $insert['is_collection']]);
        }else{
            return json_error('操作失败，请重试');
        }
    }

    //商品收藏列表
    public function goods_collection_list(BasicRequest $request){
        $this->check_login();
		
        $collection_list = ShopGoodsCollection::where(['uid'=>$this->uid,'is_collection'=>1])->get()->each(function($v,$k){
            $goods_id = $v->goods_id;
            $goods_type = $v->goods_type;
            if($goods_type==1){
                $goods_detail=ShopSeckillGoods::getGoodsDetail($goods_id);
            }else if($goods_type==2){
                $goods_detail=ShopGroupGoods::getGoodsDetail($goods_id);
            }else{
                $goods_detail=null;
            }
            if($goods_detail){
                $v->goods_image = $goods_detail->goods_image;
                $v->goods_name = $goods_detail->goods_name;
                $v->goods_price = $goods_detail->goods_price;
                $v->status = $goods_detail->status;
            }else{
                $v->status=0;
            }
        });

        return json_success('OK', $collection_list);

    }
}
